package com.otoe.dilidilivid.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

//import com.bin.david.form.annotation.SmartTable;
import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.table.TableData;
import com.lingber.mycontrol.datagridview.DataGridView;
import com.otoe.dilidilivid.R;
import com.otoe.dilidilivid.data.dao.FavDao;
import com.otoe.dilidilivid.data.dao.UserDao;
import com.otoe.dilidilivid.data.dao.VideoDao;
import com.otoe.dilidilivid.data.entity.Favorites;
import com.otoe.dilidilivid.data.entity.User;
import com.otoe.dilidilivid.data.entity.VideoInfo;

import java.util.ArrayList;
import java.util.List;

public class AdminActivity extends AppCompatActivity {

    private SmartTable table;
    private DataGridView dataGridView1;
    private DataGridView dataGridView2;
    private DataGridView dataGridView3;

    private List<User> userList;
    private List<VideoInfo> videoInfoList;
    private List<Favorites> favoritesList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        dataGridView1 = (DataGridView) findViewById(R.id.userTable);
        dataGridView2 = (DataGridView) findViewById(R.id.videoTable);
        dataGridView3 = (DataGridView) findViewById(R.id.favoritesTable);
        dataGridView2.setVisibility(View.GONE);
        dataGridView3.setVisibility(View.GONE);

        @SuppressLint("HandlerLeak") final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                userList = (ArrayList<User>) msg.obj;
                dataGridView1.setColunms(3);
                dataGridView1.setHeaderHeight(100);
                dataGridView1.setHeaderContentByStringId(new int[]{R.string.id, R.string.name, R.string.password});
                // 绑定字段
                dataGridView1.setFieldNames(new String[]{"U_id", "U_name", "Password"});
                // 每个column占比
                dataGridView1.setColunmWeight(new float[]{1, 1, 1});
                // 每个单元格包含控件
                dataGridView1.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class});
                // 设置数据源
                dataGridView1.setDataSource(userList);
                // 单行选中模式
                dataGridView1.setSelectedMode(1);
                //
                dataGridView1.setDividerSize(2);

                // 启用翻页
                //dataGridView.setFlipOverEnable(true, 9, getFragmentManager());
                // 初始化表格
                dataGridView1.initDataGridView();
            }
        };
        //从数据库获取用户信息
        new Thread() {
            @Override
            public void run() {
                UserDao userDao = new UserDao();
                List<User> userList = userDao.selectAll();
                Message message = Message.obtain();
                message.obj = userList;

                handler.sendMessage(message);
            }
        }.start();

        //视频信息
        @SuppressLint("HandlerLeak") final Handler handler2 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                videoInfoList = (ArrayList<VideoInfo>) msg.obj;
                //dataGridView.setHeaderContent(new String[]{"用户名","用户密码"});

                // 设置列数
                dataGridView2.setColunms(7);
                dataGridView2.setHeaderHeight(100);
                dataGridView2.setHeaderContentByStringId(new int[]{R.string.Vid, R.string.VTitle, R.string.VUname, R.string.VideoUrl, R.string.VPlaynums, R.string.VDuration, R.string.VImg});
                // 绑定字段
                dataGridView2.setFieldNames(new String[]{"vid", "title", "uname", "videoUrl", "playNums", "img", "duration"});
                // 每个column占比
                dataGridView2.setColunmWeight(new float[]{1, 1, 1, 1, 1, 1, 1});
                // 每个单元格包含控件
                dataGridView2.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class, TextView.class, TextView.class, TextView.class, TextView.class});
                // 设置数据源
                dataGridView2.setDataSource(videoInfoList);
                // 单行选中模式
                dataGridView2.setSelectedMode(1);
                //
                dataGridView2.setDividerSize(2);
                // 启用翻页
                dataGridView2.setFlipOverEnable(true, 15, getFragmentManager());
                // 初始化表格
                dataGridView2.initDataGridView();
            }
        };
        //从数据库获取视频信息
        new Thread() {
            @Override
            public void run() {
                VideoDao videoDao = new VideoDao();
                List<VideoInfo> videoInfoList = videoDao.selectAll();
                Message message = Message.obtain();
                message.obj = videoInfoList;
                handler2.sendMessage(message);
            }
        }.start();

        //收藏夹信息
        @SuppressLint("HandlerLeak") final Handler handler3 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                favoritesList = (ArrayList<Favorites>) msg.obj;
                //dataGridView.setHeaderContent(new String[]{"用户名","用户密码"});

                // 设置列数
                dataGridView3.setColunms(3);
                dataGridView3.setHeaderHeight(100);
                dataGridView3.setHeaderContentByStringId(new int[]{R.string.Fid,R.string.FName,R.string.Fsize});
                // 绑定字段
                dataGridView3.setFieldNames(new String[]{"F_id","F_name","Size"});
                // 每个column占比
                dataGridView3.setColunmWeight(new float[]{1, 1, 1});
                // 每个单元格包含控件
                dataGridView3.setCellContentView(new Class[]{TextView.class, TextView.class, TextView.class});
                // 设置数据源
                dataGridView3.setDataSource(favoritesList);
                // 单行选中模式
                dataGridView3.setSelectedMode(1);
                //
                dataGridView3.setDividerSize(2);
                // 启用翻页
                //dataGridView.setFlipOverEnable(true, 9, getFragmentManager());
                // 初始化表格
                dataGridView3.initDataGridView();
            }
        };
        //从数据库获取视频信息
        new Thread() {
            @Override
            public void run() {
                FavDao favoriteDao = new FavDao();
                List<Favorites> favoritesList = favoriteDao.selectAll();
                Message message = Message.obtain();
                message.obj = favoritesList;
                handler3.sendMessage(message);
            }
        }.start();

        //点击查看视频信息
        findViewById(R.id.btnVideo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dataGridView2.setVisibility(View.VISIBLE);
                dataGridView1.setVisibility(View.GONE);
                dataGridView3.setVisibility(View.GONE);
            }
        });
        //点击查看用户信息
        findViewById(R.id.btnUser).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dataGridView2.setVisibility(View.GONE);
                dataGridView1.setVisibility(View.VISIBLE);
                dataGridView3.setVisibility(View.GONE);
            }
        });
        //查看收藏夹信息
        findViewById(R.id.btnFavorite).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dataGridView2.setVisibility(View.GONE);
                dataGridView1.setVisibility(View.GONE);
                dataGridView3.setVisibility(View.VISIBLE);
            }
        });
    }

    public void showVideo() {

    }

    //初始化表格1
    public void initTable1() {
        //普通列
        Column<String> column1 = new Column<>("用户ID", "U_id");
        Column<Integer> column2 = new Column<>("用户名", "U_name");
        Column<Long> column3 = new Column<>("用户密码", "Password");
        //组合列
        Column totalColumn1 = new Column("组合列名", column1, column2);

        //表格数据 datas是需要填充的数据
        final TableData<User> tableData = new TableData<>("USERS", userList, column1, column2, column3);
        //设置数据
        //table = (SmartTable) findViewById(R.id.userTable);
        //table.setZoom(true,3);是否缩放
        table.setTableData(tableData);
    }
}